﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Service;
using CarolLib;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace DispatchAssist.AppHost
{
    public partial class frmLogin : Form
    {
        #region Members.

        Seat CurrentSeat;

        Sunisoft.IrisSkin.SkinEngine skinEngine1;

        private Seat GetCurrentSeat()
        {
            Seat seat = null;

            string mac = SysInfoHelper.GetMacAddress();

            try
            {
                seat = new AuthService().GetSeatByMac(mac);
            }
            catch (Exception e)
            {
                MessageBox.Show("系统出错，错误信息：" + e.Message);
            }

            return seat;
        }

        #endregion

        #region Ctor.

        public frmLogin()
        {
            InitializeComponent();

            skinEngine1 = SkinConfig.Instance.EnsureSkin(this);
        }

        #endregion

        #region Event Handlers

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //add at 2011-8-19 by wuzhongqi
            try
            {
                int clientCount = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count();

                if (clientCount > 1 && new AuthService().SeatUserIsLogin(this.CurrentSeat.SeatID, tbUserName.Text.Trim()))
                {
                    MessageBox.Show("该用户已在当前席位登录了,请换一个用户登录!");
                    return;
                }
            }
            catch
            {

            }


            if (AuthContext.Login(this.tbUserName.Text.Trim(), this.tbPassword.Text.Trim(), this.CurrentSeat))
            {
                this.Hide();
                frmMain main = new frmMain();
                main.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.CurrentSeat = GetCurrentSeat();
            if (this.CurrentSeat == null)
            {
                MessageBox.Show("当前计算机不属于注册席位，系统即将退出\r\n您的MAC地址为 " + SysInfoHelper.GetMacAddress());
                Application.Exit();
            }
            else
            {
                this.lblSeatName.Text = this.CurrentSeat.SeatName;
                this.tbUserName.Focus();
            }

            SkinConfig.Instance.UpdateSkin(this, this.skinEngine1);
        }

        #endregion

        private void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ((char)13))
            {
                btnLogin_Click(null, null);
            }
        }
    }
}
